package com.ithao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ithao.dao.CompanyDao;
import com.ithao.dao.DictDao;
import com.ithao.dao.IndustryDao;
import com.ithao.dao.UserDao;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.pojo.Company;
import com.ithao.pojo.Dict;
import com.ithao.pojo.Industry;
import com.ithao.pojo.User;
import com.ithao.service.CompanyService;
import com.ithao.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：seanyang
 * @date ：Created in 2019/8/15
 * @description ：公司业务实现类
 * @version: 1.0
 */
@Slf4j
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private DictDao dictDao;

    @Override
    public List<Company> findListAll() {
        return companyDao.selectListAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<Company> page = companyDao.selectPage(queryPageBean.getQueryParams());
        //  详细封装Company集合对象
        List<Company> companyList = page.getResult();
        for (Company company : companyList) {
            User createUser = userDao.findById(company.getUserId());
            company.setCreateUserName(createUser.getUsername());
            Dict dict = dictDao.findById(company.getCityId());
            company.setCity(dict.getDataValue());
        }
        log.info("[companyList]: {}", companyList);
        return new PageResult(page.getTotal(), companyList);
    }

    /**
     * 企业的启用禁用
     *
     * @param id
     */
    @Override
    public void isDis(Integer id) {
        if (id == null) {
            return;
        }
        Company company = companyDao.findById(id);
        if (null != company) {
            String state = company.getState();
            if ("1".equals(state)) {
                companyDao.updateState(id, "0");
            } else companyDao.updateState(id, "1");


        }
    }

    @Override
    public Boolean add(Company company, Integer[] industryIds) {
        //封装Company信息
        company.setUserId(1);
        company.setCreateDate(DateUtils.parseDate2String(new Date()));
        company.setState("1"); //默认启用
        //添加数据库
        int addResult = companyDao.add(company);
        if (addResult == 1) {
            log.info("[添加企业成功]");
            //插入该企业与方向的中间表
            for (Integer industryId : industryIds) {
                log.info("[插入企业的方向] companyId={},industryId={}",company.getId(),industryId);
                companyDao.insertComAndIndus(company.getId(),industryId);
            }
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        log.info("删除企业 id:{}", id);
        companyDao.deleteCompanyIndustryByCompanyId(id);
        companyDao.delete(id);
    }
}








